package com.ck.ips.controller;

import com.ck.common.model.OperationResult;
import com.ck.core.annotation.LoginRequired;
import com.ck.core.constant.PageConstant;
import com.ck.core.util.DateUtils;
import com.ck.core.util.HttpUtils;
import com.ck.ips.service.DeviceUpgradeService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.text.ParseException;

/**
 * @description: 终端apk相关controller
 * @author: zhanglei
 * @date: 2019/5/14 11:29
 */

@LoginRequired
@Controller
@RequestMapping(value = "/DeviceUpgrade")
public class DeviceUpgradeController {

    @Autowired
    private DeviceUpgradeService deviceUpgradeService;
    /**
     * 根据条件获取apk列表
     * @param versionName apk版本名称
     * @param upgradeName apk名称
     * @param pageNum 当前页
     * @param pageSize 每页条数
     * @return apk列表
     */
    @ResponseBody
    @PostMapping(value = "/getApkList")
    public Object getApkList(String versionName,String upgradeName,Integer pageNum,Integer pageSize){
        pageNum = pageNum == null ? PageConstant.DEFAULT_PAGE_NUM : pageNum;
        pageSize = pageSize == null ? PageConstant.DEFAULT_PAGE_SIZE : pageSize;
        return deviceUpgradeService.getApkList(versionName,upgradeName,pageNum,pageSize);
    }

    /**
     * 上传apk
     * @param files 文件
     * @return 解析数据
     */
    @ResponseBody
    @PostMapping(value = "/uploadApk")
    public Object uploadApk(@RequestParam("files") MultipartFile files) throws IOException {
        return deviceUpgradeService.uploadApk(files);
    }

    /**
     * 新增apk
     * @param upgradeName 文件名称
     * @param versionCode 版本号
     * @param versionName 版本名称
     * @param packageName 包名
     * @param md5 MD5
     * @param size 大小
     * @param url 下载地址
     * @param active 是否强制升级
     * @param isTiming 是否定时升级
     * @param timingTime 定时升级时间
     * @param description 版本描述
     * @return
     */
    @ResponseBody
    @PostMapping(value = "/addApk")
    public Object addApk(String upgradeName,Integer versionCode,String versionName,String packageName,String md5,String size,String url,Integer active,Integer isTiming,String timingTime,String description){
        Long  timingTimeNum = null;
        if(!StringUtils.isBlank(timingTime)){
            try {
                timingTimeNum = DateUtils.stringTimeToLongTimeStamp(timingTime,DateUtils.PATTEN_YMD_HMS);
            }catch (ParseException e){
                return OperationResult.newValidateFailOperationResult("时间格式错误");
            }
        }
        return deviceUpgradeService.addApk(upgradeName,versionCode,versionName,packageName,md5,size,url,active,isTiming,timingTimeNum,description);
    }

    /**
     * 根据id获取apk详情
     * @param deviceUpgradeId apk的id
     * @return apk详情
     */
    @ResponseBody
    @PostMapping(value = "/getApkById")
    public Object getApkById(Long deviceUpgradeId){
        return deviceUpgradeService.getApkById(deviceUpgradeId);
    }

    /**
     * 编辑apk
     * @param deviceUpgradeId apk id
     * @param upgradeName 文件名称
     * @param versionCode 版本号
     * @param versionName 版本名称
     * @param packageName 包名
     * @param md5 MD5
     * @param size 大小
     * @param url 下载地址
     * @param active 是否强制升级
     * @param isTiming 是否定时升级
     * @param timingTime 定时升级时间
     * @param description 版本描述
     * @return
     */
    @ResponseBody
    @PostMapping(value = "/editApk")
    public Object editApk(Long deviceUpgradeId,String upgradeName,Integer versionCode,String versionName,String packageName,String md5,String size,String url,Integer active,Integer isTiming,String timingTime,String description){
        Long  timingTimeNum = null;
        if(!StringUtils.isBlank(timingTime)){
            try {
                timingTimeNum = DateUtils.stringTimeToLongTimeStamp(timingTime,DateUtils.PATTEN_YMD_HMS);
            }catch (ParseException e){
                return OperationResult.newValidateFailOperationResult("时间格式错误");
            }
        }
        return deviceUpgradeService.editApk(deviceUpgradeId,upgradeName,versionCode,versionName,packageName,md5,size,url,active,isTiming,timingTimeNum,description);
    }

    /**
     * 删除apk
     * @param deviceUpgradeId apk id
     * @return
     */
    @ResponseBody
    @PostMapping(value = "/deleteApk")
    public Object deleteApk(Long deviceUpgradeId){
        return deviceUpgradeService.deleteApk(deviceUpgradeId);
    }

    /**
     * 手动升级apk
     * @param deviceCodes 终端code字符串
     * @param deviceUpgradeId apkid
     * @return
     */
    @ResponseBody
    @PostMapping(value = "/upgradeApkToMips")
    public Object upgradeApkToMips(String deviceCodes, Long deviceUpgradeId, HttpServletRequest request){
        return deviceUpgradeService.upgradeApkToMips(deviceCodes,deviceUpgradeId, HttpUtils.getRemortIP(request));
    }


    /**
     * 查看apk升级详情
     * @param deviceName 终端名称
     * @param deviceSerialNum 终端序列号
     * @param deviceUpgradeId apk id
     * @param pageNum 当前页
     * @param pageSize 每页条数
     * @return
     */
    @ResponseBody
    @PostMapping(value = "/deviceUpgradeInfo")
    public Object deviceUpgradeInfo(String deviceName,String deviceSerialNum,Long deviceUpgradeId,Integer pageNum,Integer pageSize){
        pageNum = pageNum == null ? PageConstant.DEFAULT_PAGE_NUM : pageNum;
        pageSize = pageSize == null ? PageConstant.DEFAULT_PAGE_SIZE : pageSize;
        return deviceUpgradeService.deviceUpgradeInfo(deviceName,deviceSerialNum,deviceUpgradeId,pageNum,pageSize);
    }

    /**
     * 查看apk升级数量详情
     * @param deviceUpgradeId apk 的 id
     * @return 数量详情
     */
    @ResponseBody
    @PostMapping(value = "/getApkUpgradeNumDetail")
    public Object getApkUpgradeNumDetail(Long deviceUpgradeId){
        return deviceUpgradeService.getApkUpgradeNumDetail(deviceUpgradeId);
    }





}
